package org.apache.tapestry5.internal.portlet.services.response;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.portlet.MimeResponse;
import javax.portlet.PortletURL;
import javax.portlet.ResourceURL;

import org.apache.tapestry5.services.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// TODO: remove abstract (both keyword and from class name)
public abstract class AbstractMimePortletResponse extends AbstractPortletResponse {

    private final Logger         _logger  = LoggerFactory.getLogger(PortletRenderResponse.class);

    protected final MimeResponse _response;

    private boolean              commited = false;

    public AbstractMimePortletResponse(MimeResponse response, Request request) {
        super(response, request);

        this._response = response;
    }

    public void reset() {
        this._response.reset();
    }

    @Override
    public PrintWriter getPrintWriter(String contentType) throws IOException {
        this._logger.info("getPrintWriter({})", contentType);
        this._response.setContentType(contentType.toString());
        this.commited = true;
        return this._response.getWriter();
    }

    public String getNamespace() {
        return this._response.getNamespace();
    }

    @Override
    public boolean isCommitted() {
        this._logger.info("isCommited");
        return this.commited || this._response.isCommitted();
    }

    @Override
    public OutputStream getOutputStream(String contentType) throws IOException {
        this._logger.info("getOutputStream");
        this._response.setContentType(contentType);
        this.commited = true;
        return this._response.getPortletOutputStream();
    }

    @Override
    public PortletURL createRenderURL() {
        return this._response.createRenderURL();
    }

    @Override
    public PortletURL createActionURL() {
        return this._response.createActionURL();
    }

    @Override
    public ResourceURL createResourceURL() {
        return this._response.createResourceURL();
    }

}